package com.ctrip.study.algorithm.queue;

public class MyArrayQueue<T> implements MyQueue<T>{
	
	private int maxSize;
	private Object[] array;
	//队首指针
	private int front;
	//队尾指针
	private int rear;
	//当前队列中的个数
	private int nItem;
	
	

	public MyArrayQueue(int maxSize) {
		this.maxSize = maxSize;
		this.array = new Object[maxSize];
		this.front = 0;
		this.rear = -1;
		this.nItem = 0;
	}

	@Override
	public boolean isEmpty() {
		return nItem == 0;
	}

	@Override
	public boolean isFull() {
		return nItem == maxSize;
	}

	@Override
	public void insert(T t) {
		if(rear == maxSize - 1){
			rear = -1;
		}
		rear++; //队尾指针+1
		array[rear] = t;
		nItem++; //当前数据+1
	}

	@Override
	public T remove() {
		T data = (T) array[front];
		front++;
		if(front == maxSize){
			front = 0;
		}
		nItem--;
		return data;
	}

	@Override
	public int size() {
		return nItem;
	}

}
